package com.kmfish.util;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class MyLock extends Activity {
    
	
	private Button lockButton;
	
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                
        setContentView(R.layout.main);
        lockButton = (Button) findViewById(R.id.lockButton);
		
        lockButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				goToSleep(MyLock.this, 10000);
			}
		});
        
    }
	
	//添加屏幕待机方法
    public void goToSleep(final Context context, final long time) {
    	
    	context.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
        final AlertDialog dialog = new AlertDialog.Builder(context)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("sleep")
        .setMessage("sleep_confirm")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which){
		            PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
		            pm.goToSleep(time);
               
	               try {
	            	   Thread.sleep(10000);
	               } catch (InterruptedException e) {
	            	   // TODO Auto-generated catch block
	            	   e.printStackTrace();
	               }

	            }
           })
          .setNegativeButton(android.R.string.no, null)
          .create();
        
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialog.show();
     
      }
	
	
}